package org.exteen.exec;

import org.exteen.output.ExteenStream;
import org.exteen.parser.Attr;
import org.exteen.parser.Node;

public class DynamicTag extends Tag {

    private final Node root;
    
    public DynamicTag(Node root) {
        this.root = root;
    }
    
    public void doTag(ExteenContext ctx, ExteenStream stream) {
        DynamicContext dcx = new DynamicContext(ctx, root);
        copyParametersAsVariables(ctx.getNode(), dcx);
        super.doBody(dcx, stream);
    }

    private void copyParametersAsVariables(Node node, ExteenContext ctx) {
        for (Attr a: node.getAttributes()) {
            String key = a.getKey();
            Object value = a.getValue(ctx);
            if (value!=null) {
                ctx.set(key, value, false);
            }
        }
    }
}